package com.situ.web.controller;

import com.situ.web.pojo.Student;
import com.situ.web.service.IStudentService;
import com.situ.web.service.impl.StudentServiceImpl;
import com.situ.web.util.JDBCUtil;
import com.situ.web.util.JSONResult;
import com.situ.web.util.JSONUtil;
import com.situ.web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// http://localhost:8080/JavaWeb/index.jsp
// http://localhost:8080/JavaWeb/student
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private IStudentService studentService = new StudentServiceImpl();

    // http://localhost:8080/JavaWeb/student?method=selectAll
    // http://localhost:8080/JavaWeb/student?method=deleteById&id=1

    //找到Servlet之后，默认访问service方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentServlet.service");
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectAll":
                selectAll(req, resp);
                break;
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
        }
    }

    // /student?method=selectByPage&pageNo=2&pageSize=5
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentServlet.selectByPage");
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
        if (pageNo == null || pageNo.equals("")) {
            pageNo = "1";
        }
        if (pageSize == null || pageSize.equals("")) {
            pageSize = "5";
        }
        PageInfo pageInfo = studentService.selectByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        System.out.println("pageInfo: " + pageInfo);
        req.setAttribute("pageInfo", pageInfo);
        req.getRequestDispatcher("student_list_page.jsp").forward(req, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("StudentServlet.add");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String banjiId = req.getParameter("banjiId");
        Student student = new Student(name, Integer.parseInt(age), gender, Integer.parseInt(banjiId));
        studentService.add(student);

        //重定向  302
        //让浏览器拿到这个路径之后，浏览器自己发送这个请求
        //resp.sendRedirect(req.getContextPath() + "/student?method=selectByPage");

        JSONUtil.obj2Json(JSONResult.ok("添加成功"), resp);
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("StudentServlet.deleteById");
        String id = req.getParameter("id");

        studentService.deleteById(Integer.parseInt(id));

        //重定向  302
        //让浏览器拿到这个路径之后，浏览器自己发送这个请求
        // resp.sendRedirect(req.getContextPath() + "/student?method=selectByPage");

        JSONUtil.obj2Json(JSONResult.ok("删除成功"), resp);
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentServlet.selectAll");
        List<Student> list = studentService.selectAll();

        //把数据放到req这个内存里面
        req.setAttribute("list", list);
        //转发到student_list.jsp页面进行展示  forward：转发
        req.getRequestDispatcher("student_list.jsp").forward(req, resp);
    }
}
